package com.itheima.listener;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

/**
 * @author zero
 * @date 2020/6/16
 */


/**
 * Consumer prefetch限流机制：
 * 1. 确保ack机制为手动确认。
 * 2. listener-container配置属性
 * perfetch = 1,表示消费端每次从mq拉去一条消息来消费，直到手动确认消费完毕后，才会继续拉去下一条消息。
 */
@Component
public class QosListener implements ChannelAwareMessageListener {

    @Override
    public void onMessage(Message message, com.rabbitmq.client.Channel channel) throws Exception {

        Thread.sleep(1000);
        //1.接收消息
        System.out.println(new String(message.getBody()));
        //2.处理业务逻辑
        //System.out.println("处理业务逻辑");
        //3.手动签收
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);

    }
}
